package com.nufront.bloodsugar.model;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nufront.bloodsugar.R;

public class DragButton extends FrameLayout {

	private int currentTab = -1;

	private TextView mButton;
	private int mButtonLeft;

	private int widgetWidth;
	private int width;
	private int textButtonWidth;

	private OnClickListener textOnClickListener;
	private OnClickListener textOffClickListener;

	private TextView btOn;
	private TextView btOff;

	private boolean right;

	private int firstX;
	private Drawable txtBtBg;

	public DragButton(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	public DragButton(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
		// TODO Auto-generated constructor stub
		TypedArray a = context.obtainStyledAttributes(attrs,
				R.styleable.MyDragButton);
		txtBtBg = a.getDrawable(R.styleable.MyDragButton_txtButtonBg);

	}

	public DragButton(Context context) {
		this(context, null);
		// TODO Auto-generated constructor stub
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub

		final int action = event.getAction();
		final int moveX = (int) event.getX();
		final int scape = moveX - firstX;

		switch (action) {
		case MotionEvent.ACTION_DOWN:
			firstX = (int) event.getX();
			break;
		case MotionEvent.ACTION_MOVE:
			move(scape);
			break;
		case MotionEvent.ACTION_UP:
			if (currentTab == 1) {
				if (mButtonLeft != 0) {
					animationStart(-mButtonLeft, 0);
				}
			} else if (currentTab == 2) {
				if (mButtonLeft != width) {
					animationStart(mButtonLeft, width);
				}
			}
			break;
		default:
			break;
		}

		return true;
	}

	private void animationStart(int left, int leftMargin) {
		TranslateAnimation translateAnimation = new TranslateAnimation(
				Animation.RELATIVE_TO_SELF, 0, Animation.ABSOLUTE,
				left / width, Animation.RELATIVE_TO_SELF, 0,
				Animation.RELATIVE_TO_SELF, 0);
		translateAnimation.setStartOffset(0);
		translateAnimation.setDuration(100);
		translateAnimation.setFillBefore(false);
		mButton.startAnimation(translateAnimation);
		setLayoutLeftMargin(leftMargin);
	}

	public void setCurrentTab(int curTab, boolean isSetLayoutParams) {
		if (currentTab == curTab) {
			return;
		}

		currentTab = curTab;
		if (curTab == 1 && isSetLayoutParams) {
			btOn.setVisibility(View.INVISIBLE);
			btOff.setVisibility(View.VISIBLE);
			setLayoutLeftMargin(0);
		} else if (curTab == 2 && isSetLayoutParams) {
			btOn.setVisibility(View.VISIBLE);
			btOff.setVisibility(View.INVISIBLE);
			setLayoutLeftMargin(width);
		}

	}

	public void setCurrentTab(int curTab) {
		setCurrentTab(curTab, false);
	}

	private void move(int scape) {
		LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) mButton
				.getLayoutParams();
		mButtonLeft = mButton.getLeft();
		if (width <= scape) {
			layoutParams.leftMargin = 90;
		} else if (scape < 0) {
			if (mButtonLeft == width) {
				right = true;
			} else if (mButtonLeft == 0) {
				right = false;
			}

			System.out.println("right = " + right);

			if (right) {
				layoutParams.leftMargin = width + scape;
				if (layoutParams.leftMargin <= 0) {
					layoutParams.leftMargin = 0;
				}
			} else {
				layoutParams.leftMargin = scape;
				if (layoutParams.leftMargin <= 0) {
					layoutParams.leftMargin = 0;
				}
			}
		} else if (scape > 0 && width > scape && !right) {
			layoutParams.leftMargin = scape;
			if (mButtonLeft == width) {
				layoutParams.leftMargin = width;
			}
		}
		if (widgetWidth <= mButtonLeft + textButtonWidth / 2) {
			setCurrentTab(2);
		} else if (widgetWidth >= mButtonLeft + textButtonWidth / 2) {
			setCurrentTab(1);
		}

		mButton.setLayoutParams(layoutParams);
	}

	@Override
	protected void onFinishInflate() {
		// TODO Auto-generated method stub

		mButton = (TextView) findViewById(R.id.mButton);
		mButton.setBackgroundDrawable(txtBtBg);
		btOn = (TextView) findViewById(R.id.bt1);
		btOff = (TextView) findViewById(R.id.bt2);
		LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mButton
				.getLayoutParams();
		textButtonWidth = lp.width;
		widgetWidth = 90;
		width = (int) (widgetWidth / 1.0);
		super.onFinishInflate();
	}

	private void setLayoutLeftMargin(int leftMargin) {
		LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mButton
				.getLayoutParams();
		lp.leftMargin = leftMargin;
		mButton.setLayoutParams(lp);
	}

	public void setTextOnClickListener(OnClickListener listener) {
		textOnClickListener = listener;
		this.btOn.setOnClickListener(textOnClickListener);
	}

	public void setTextOffClickListener(OnClickListener listener) {
		textOffClickListener = listener;
		this.btOff.setOnClickListener(textOffClickListener);
	}

}
